<?php

define("sDirLanguage", App::getParam('path') . "file/language/");
define("sProduct", "konsort");

set_time_limit(0);

$oDb = &Database::get();

$oReq = &App::getRequest();

$oTpl = &App::getTemplate();

App::loadClass('Validator');

$modules = App::getParam('modules');

if ( $oReq->get('submit') )
{
	$aPost = $oReq->getArray('val');
	$sImport = $oReq->get('import');

	$aLanguageQuery = array
	(
		'title' => $aPost['title'],
		'userselect' => $aPost['userselect'],
		'languagecode' => $aPost['languagecode'],
		'charset' => $aPost['charset'],
		'direction' => $aPost['direction'],
		'time' => time(),
		'created' => $aPost['created'],
		'site' => $aPost['site'],
	);

	$iId = $oDb->insert(App::getT('language'), $aLanguageQuery);

	if ( !$iId )
	{
		trigger_error("Cant insert language package", E_USER_ERROR);
	}

	$iDefaultId = 1;

	require_once sDirLanguage . ''. $sImport .'/core/mess.1.php';
	require_once sDirLanguage . ''. $sImport .'/core/mess.2.php';

	$iTotalPhrases = 0;
	foreach($_MSG as $m => $v)
	{
		$iTotalPhrases++;

		$s = explode(".", $m);

		$query = array
		(
			'languageid' => $iId,
			'varname' => $m,
			'text' => $oDb->escape($v),
			'product' => sProduct,
			'time' => time(),
			'version' => App::getParam('info.version'),
			'module' => (in_array($s[0], $modules) ? $s[0] : ''),
			'default' => $oDb->escape($v),
		);

		$oDb->insert(App::getT('language_phrase'), $query);

		flush();
	}

	$aPhraseCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('language_phrase') ." WHERE `languageid` = '". $iDefaultId ."'");
	$iMissingPhrases = ($aPhraseCnt - $iTotalPhrases);

	$aOptions = array
	(
		'poem',
		'image_cat',
		'event_items',
		'country',
		'us_stat',
		'day',
		'month',
		'jmood',
	);

	$iOptionsImported = 0;
	foreach($aOptions as $sOption)
	{
		if ( file_exists(sDirLanguage . ''. $sImport .'/'. $sOption .'.html') )
		{
			$file = file(sDirLanguage . ''. $sImport .'/'. $sOption .'.html');

			foreach($file as $item)
			{
				$item = trim($item);
				$iOptionsImported++;
				$query = array
				(
					'languageid' => $iId,
					'varname' => $sOption,
					'text' => $item,
					'default' => $item,
					'product' => sProduct,
					'time' => time(),
				);

				$oDb->insert(App::getT('language_options'), $query);

				flush();
			}
		}
	}

	$aOptionCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('language_options') ." WHERE `languageid` = '". $iDefaultId ."'");
	$iMissingOption = ($aOptionCnt - $iOptionsImported);

	$sDir = sDirLanguage . ''. $sImport .'/';
	$mDir = opendir($sDir);
	$iContentImported = 0;
	while( $sFile = readdir($mDir) )
	{
		if ( substr($sFile, -4) == 'html'
			&& $sFile != 'index.html'
			&& !in_array(str_replace(".html", "", $sFile), $aOptions)
			&& substr($sFile, 0, 4) != 'ban_'
		)
		{
			$iContentImported++;

			$sContent = file_get_contents($sDir . $sFile);

			$aQuery = array
			(
				'languageid' => $iId,
				'varname' => "site_content_" . str_replace(".html", "", $sFile),
				'text' => $sContent,
				'product' => sProduct,
				'time' => time(),
				'version' => App::getParam('info.version'),
				'module' => 'Mod_Messenger',
				'default' => $sContent,
			);

			$oDb->insert(App::getT('language_phrase'), $aQuery);

			flush();
		}
	}

	$aContentCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('language_phrase') ." WHERE `varname` LIKE 'site_content_%' AND `languageid` = '". $iDefaultId ."'");
	$iMissingContent = ($aContentCnt - $iContentImported);

	$iTotalMissing = ($iMissingContent + $iMissingOption + $iMissingPhrases);

	App::rmCache('language_listing.php');

	if ( !$iTotalMissing )
	{
		App::gotoUrl('admin.language.import', array(), App::format('Mod_Language.import_complete'), false);
	}
}

$aLanguages = array();
if ( is_dir(sDirLanguage) )
{
	$mDir = opendir(sDirLanguage);
	while ( $sFile = readdir($mDir) )
	{
		if ( ($sFile != '.' && $sFile != '..') && (file_exists(sDirLanguage . $sFile ."/_config.php")) )
		{
			$aLanguages[] = $sFile;
		}
	}
}

$aFields = array
(
	'val[title]'   => array('title' => App::format('language_title'), 'def' => 'required'),
	'val[languagecode]'   => array('title' => App::format('laguage_abb_code'), 'def' => 'required'),
	'val[charset]'   => array('title' => App::format('language_charset'), 'def' => 'required'),
);
$oValidator = &new Validator($aFields, array());

$oTpl->assignSrc('sValMess', $oValidator->makeJsMess());
$oTpl->assignSrc('sValJs', $oValidator->makeJS());
$oTpl->assign(array
(
	'aLanguages' => $aLanguages,
	'iTotalMissing' => (isset($iTotalMissing) ? $iTotalMissing : ''),
	'iId' => (isset($iId) ? $iId : ''),
));

?>